<?php
	class AutoNumber extends CComponent{		
		static public function getMonthlyFeeBulkNo(){
			$preBulkNo = "BLKM";
			$th = date("Y");
			$preBulk = $th.$preBulkNo;			
			$inv = MStudentHInvoice::model()->find("bulk_no like '$preBulk%' order by bulk_no desc");
			if ($inv != null){			
				$lastBulkNo = $inv->bulk_no;			
				$tmp = substr($lastBulkNo, strlen($preBulk));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;				
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preBulk . $nextNumber;
			}else{
				return $preBulk . "000001";
			} 			
		}
		static public function getNewStudentInvoiceNo($lastNo = null){
			$pre = "SINV";
			$th = date("Y");
			$preFull = $th.$pre;
			
			if ($lastNo == null){
				//$inv = MStudentInvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				$inv = MStudentHinvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				if ($inv != null){
					$lastNo = $inv->invoice_no;
					$tmp = substr($lastNo, strlen($preFull));
					$tmpNumber = (int)$tmp;
					$tmpNumber = $tmpNumber + 1;		
					$nextNumber = "000000" . $tmpNumber;
					$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
					return $preFull . $nextNumber;
				}else{
					return $preFull . "000001";
				}
			}else{
				$tmp = substr($lastNo, strlen($preFull));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;		
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preFull . $nextNumber;
			}
		}
		static public function getJurnalNo($lastNo = null){
			$pre = AppSetting::getValue("jurnalPrefix");
			$th = date("Y");
			$preFull = $th.$pre;
				
			if ($lastNo == null){
				//$inv = MStudentInvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				$jurnal = MHjurnal::model()->find("no_jurnal like '$preFull%' order by no_jurnal desc");
				if ($jurnal != null){
					$lastNo = $jurnal->no_jurnal;
					$tmp = substr($lastNo, strlen($preFull));
					$tmpNumber = (int)$tmp;
					$tmpNumber = $tmpNumber + 1;
					$nextNumber = "000000" . $tmpNumber;
					$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
					return $preFull . $nextNumber;
				}else{
					return $preFull . "000001";
				}
			}else{
				$tmp = substr($lastNo, strlen($preFull));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preFull . $nextNumber;
			}
		}
		static public function getNewStudentPaymentNo($lastNo = null){
			$pre = "SPYM";
			$th = date("Y");
			$preFull = $th.$pre;				
			if ($lastNo == null){
				//$pym = MStudentInvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				$pym = MStudentHpayment::model()->find("payment_no like '$preFull%' order by payment_no desc");
				if ($pym != null){
					$lastNo = $pym->payment_no;
					$tmp = substr($lastNo, strlen($preFull));
					$tmpNumber = (int)$tmp;
					$tmpNumber = $tmpNumber + 1;
					$nextNumber = "000000" . $tmpNumber;
					$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
					return $preFull . $nextNumber;
				}else{
					return $preFull . "000001";
				}
			}else{
				$tmp = substr($lastNo, strlen($preFull));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preFull . $nextNumber;
			}
		}
	
		static public function getNewCustomerInvoiceNo($lastNo = null){
			$pre = "CINV";
			$th = date("Y");
			$preFull = $th.$pre;
				
			if ($lastNo == null){
				//$inv = MStudentInvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				$inv = MCustomerHinvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				if ($inv != null){
					$lastNo = $inv->invoice_no;
					$tmp = substr($lastNo, strlen($preFull));
					$tmpNumber = (int)$tmp;
					$tmpNumber = $tmpNumber + 1;
					$nextNumber = "000000" . $tmpNumber;
					$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
					return $preFull . $nextNumber;
				}else{
					return $preFull . "000001";
				}
			}else{
				$tmp = substr($lastNo, strlen($preFull));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preFull . $nextNumber;
			}
		}
		static public function getNewCustomerPaymentNo($lastNo = null){
			$pre = "CPYM";
			$th = date("Y");
			$preFull = $th.$pre;
			if ($lastNo == null){
				//$pym = MStudentInvoice::model()->find("invoice_no like '$preFull%' order by invoice_no desc");
				$pym = MCustomerHpayment::model()->find("payment_no like '$preFull%' order by payment_no desc");
				if ($pym != null){
					$lastNo = $pym->payment_no;
					$tmp = substr($lastNo, strlen($preFull));
					$tmpNumber = (int)$tmp;
					$tmpNumber = $tmpNumber + 1;
					$nextNumber = "000000" . $tmpNumber;
					$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
					return $preFull . $nextNumber;
				}else{
					return $preFull . "000001";
				}
			}else{
				$tmp = substr($lastNo, strlen($preFull));
				$tmpNumber = (int)$tmp;
				$tmpNumber = $tmpNumber + 1;
				$nextNumber = "000000" . $tmpNumber;
				$nextNumber = substr($nextNumber, strlen($nextNumber)-6);
				return $preFull . $nextNumber;
			}
		}
	}
?>